﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Zero.POCO.Compagne;
using Zero.Presenter;
using Zero.Presenter.Donnees;
using TypeMedia = Zero.POCO.Compagne.TypeMedia;

namespace DevExpress.MailClient.Win.Forms
{
    public partial class EditMapTarif : Form
    {
        public TypeMedia _media ;
        public string Support;
        private bool _isMapTarifModified;
        public  ModelViewMapTarif MapSupportTarif;

        public EditMapTarif(MapSupportTarif mapSupportTarif, bool newobject, List<TarifTv> tariftvs, List<TarifRd> tarifrds, List<TarifPr> tarifPrs, List<String> Support,TypeMedia media ,String caption = "")
        {
            InitializeComponent();
            _media = media;
            if (Support != null)
                sluSupport.Properties.DataSource = Support;
           
            switch (media)
            {
                case TypeMedia.TV:
                {
                    sluGrille.Properties.DataSource = tariftvs;
                    sluGrille.Properties.DisplayMember = "nom";
                    break;
                }
                case TypeMedia.RD:
                {
                    sluGrille.Properties.DataSource = tarifrds;
                    sluGrille.Properties.DisplayMember = "nom";
                    break;
                }
                case TypeMedia.PR:
                {
                    sluGrille.Properties.DataSource = tarifPrs;
                    sluGrille.Properties.DisplayMember = "nom";
                    break;
                }
            }

            MapSupportTarif=new ModelViewMapTarif();
            
            if (newobject)
                MapSupportTarif.id = default(Guid);
            else
                MapSupportTarif.id = mapSupportTarif.id;
        }


        DialogResult QueryClose()
        {
            if (!_isMapTarifModified)
            {
                DialogResult=DialogResult.OK;
                Close();
            }

            DialogResult result = XtraMessageBox.Show(this, Properties.Resources.SaveQuestion, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
            switch (result)
            {
                case DialogResult.Cancel:
                {
                    DialogResult = DialogResult.Cancel;
                    Close();
                    return DialogResult.Cancel;
                }
                case DialogResult.No:
                {
                    DialogResult = DialogResult.No;
                    Close();
                    return DialogResult.No;
                }
                default:
                    ApplyChanges();
                    DialogResult=DialogResult.Yes;
                    Close();
                    return DialogResult = DialogResult.Yes;
            }


        }

        private void ApplyChanges()
        {

            switch (_media)
            {
                case TypeMedia.TV:
                {
                    MapSupportTarif.GrilleTarifaire=((TarifTv) sluGrille.EditValue).nom;
                    MapSupportTarif.GrilleTv = ((TarifTv)sluGrille.EditValue);
                    break;
                }
                case TypeMedia.RD:
                {
                    MapSupportTarif.GrilleTarifaire = ((TarifRd)sluGrille.EditValue).nom;
                    MapSupportTarif.GrilleRd = ((TarifRd)sluGrille.EditValue); 
                    break;
                }
                case TypeMedia.PR:
                {
                    MapSupportTarif.GrilleTarifaire = ((TarifPr)sluGrille.EditValue).nom;
                    MapSupportTarif.Grillepr = ((TarifPr)sluGrille.EditValue); 
                    break;
                }
            }
            MapSupportTarif.Support=new ModelSupport {Name = (string) sluSupport.EditValue};
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            _isMapTarifModified = false;
            ApplyChanges();
            DialogResult = DialogResult.OK;
            Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void sluSupport_EditValueChanged(object sender, EventArgs e)
        {
            _isMapTarifModified = true;
        }

        private void sluGrille_EditValueChanged(object sender, EventArgs e)
        {
            _isMapTarifModified = true;
        }
    }
}
